//
//  MoyaTests1ViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/23.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import Moya

class MoyaTests1ViewController: UIViewController {

    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "46.1.将结果转为 JSON 对象"
        view.backgroundColor = .white
        
        test()
    }
    
    
    // 转json1
    func test() {
        //获取数据
        DouBanProvider.rx.request(.channels)
            .subscribe(onSuccess: { response in
                //数据处理
                let json = try? response.mapJSON() as! [String: Any]
                print("--- 请求成功！返回的如下数据 ---")
                print(json!)
            },onError: { error in
                print("数据请求失败!错误原因：", error)
                 
            }).disposed(by: disposeBag)
    }

    // 转json2
    func test2() {
        //获取数据
        DouBanProvider.rx.request(.channels)
            .mapJSON()
            .subscribe(onSuccess: { data in
                //数据处理
                let json = data as! [String: Any]
                print("--- 请求成功！返回的如下数据 ---")
                print(json)
            },onError: { error in
                print("数据请求失败!错误原因：", error)
                 
            }).disposed(by: disposeBag)
    }
}
